import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-addons-test-utils';
import {List} from 'immutable';
import Voting from '../../src/components/Voting';
import {expect} from 'chai';
import S from '../SampleData'

const {renderIntoDocument, scryRenderedDOMComponentsWithTag, Simulate} = ReactTestUtils;

describe('Voting', ()=> {
    it('renders a pair of buttons', ()=> {
        const component = renderIntoDocument(
            <Voting pair={[S.trainspotting, S.later]}/>
        );
        const buttons = scryRenderedDOMComponentsWithTag(component, 'button');

        expect(buttons.length).to.equal(2);
        expect(buttons[0].textContent).to.equal(S.trainspotting);
        expect(buttons[1].textContent).to.equal(S.later);
    });
    it('invokes callback when a button si clicked', ()=> {
        let votedWith = null;
        const vote = (entry) => votedWith = entry;
        const component = renderIntoDocument(
            <Voting pair={[S.trainspotting, S.later]} vote={vote}/>
        );
        const buttons = scryRenderedDOMComponentsWithTag(component, 'button');
        Simulate.click(buttons[0]);
        expect(votedWith).to.equal(S.trainspotting);
    });
    it('disables buttons when user has voted', ()=> {
        const component = renderIntoDocument(
            <Voting pair={[S.trainspotting, S.later]} hasVoted={S.trainspotting}/>
        );
        const buttons = scryRenderedDOMComponentsWithTag(component, 'button');

        expect(buttons.length).to.equal(2);
        expect(buttons[0].hasAttribute('disabled')).to.equal(true);
        expect(buttons[1].hasAttribute('disabled')).to.equal(true);
    });
    it('adds label to the voted entry', ()=> {
        const component = renderIntoDocument(
            <Voting pair={[S.trainspotting, S.later]} hasVoted={S.trainspotting}/>
        );
        const buttons = scryRenderedDOMComponentsWithTag(component, 'button');

        expect(buttons[0].textContent).to.contain('Voted');
    });
    it('renders just the winner when there is one', ()=> {
        const component = renderIntoDocument(
            <Voting winner={S.trainspotting}/>
        );
        const buttons = scryRenderedDOMComponentsWithTag(component, 'button');
        expect(buttons.length).to.equal(0);

        const winner = ReactDOM.findDOMNode(component.refs.winner);
        expect(winner).to.be.ok;
        expect(winner.textContent).to.contain(S.trainspotting);
    });
    it('renders as a pure component', ()=> {
        const pair = [S.trainspotting, S.later];
        const component = renderIntoDocument(
            <Voting pair={pair}/>
        );

        let firstButton = scryRenderedDOMComponentsWithTag(component, 'button')[0];
        expect(firstButton.textContent).to.equal(S.trainspotting);

        pair[0] = S.sunshine;
        component.setProps({pair: pair});
        firstButton = scryRenderedDOMComponentsWithTag(component, 'button')[0];
        expect(firstButton.textContent).to.equal(S.trainspotting);
    });
    it('does update DOM when prop changes', ()=> {
        const pair = List.of(S.trainspotting, S.later);
        const component = renderIntoDocument(
            <Voting pair={pair}/>
        );

        let firstButton = scryRenderedDOMComponentsWithTag(component, 'button')[0];
        expect(firstButton.textContent).to.equal(S.trainspotting);

        const newPair = pair.set(0, S.sunshine);
        component.setProps({pair: newPair});
        firstButton = scryRenderedDOMComponentsWithTag(component, 'button')[0];
        expect(firstButton.textContent).to.equal(S.sunshine);
    });
});